package logic

import (
	"context"
	"fmt"
	"wechat/wechatrpc/models/cache"
	"wechat/wechatrpc/models/mysql"

	"wechat/wechatrpc/internal/svc"
	"wechat/wechatrpc/wechatrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *wechatrpc.RegisterRequest) (*wechatrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	get := cache.Rdb.Get(l.ctx, "SendSms"+in.SendSms).Val()
	if get != in.SendSms {
		return nil, fmt.Errorf("短信验证码错误")
	}
	var wechat mysql.Wechat
	mysql.DB.Debug().Where("account=?", in.Address).Find(&wechat)
	if wechat.ID != 0 {
		return nil, fmt.Errorf("账户已存在,请前去注册")
	}
	user := mysql.User{

		Image:   in.Image,
		Name:    in.Name,
		Address: in.Address,
	}
	mysql.DB.Debug().Create(&user)
	return &wechatrpc.RegisterResponse{
		Id: int64(user.ID),
	}, nil
}
